﻿package net.wangit.view.element.component;

import net.wangit.Page;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.support.ElementWriter;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.page.PageImpl;
import net.wangit.view.tree.Tree;
import net.wangit.view.tree.TreeConfig;
import net.wangit.view.tree.TreeProvider;

public final class TreeElement extends ElementComponent {

	private static final String TREE_JS = "/common/tree/tree.js";
	private static final ElementWriter writer = new TreeElementWriter();
	protected String treeId;

	public TreeElement() {
	}

	protected void configure(Row row) {
		treeId = row.get("r");
	}

	protected ElementWriter getWriter() {
		return writer;
	}

	protected void cloneAfter(ActionContext ac, Page page) {
		Object object = getObject();
		if (object == null && !Checker.isEmpty(treeId)) {
			String domain = getParent().getDomain();
			object = TreeProvider.getTree(domain, treeId);
			setObject(object);
		}
		if (page != null) {
			PageImpl pageImpl = (PageImpl) page;
			pageImpl.putElementHeadJsPath(TreeElement.TREE_JS);
			if (object != null) {
				TreeConfig config = ((Tree) object).getConfig();
				pageImpl.putElementCssPath(config.getSkin());
			}
		}
	}

}
